using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_SlideButton), true)]
public class HMI_SlideButton_Editor : HMI_EditorBase
{
    private HMI_SlideButton mTarget;
    private SlideButton mSliceButton;
    private RectTransform mLayoutRoot;
    private RectTransform mBar;
    private SerializedProperty Texts;

    private bool hasArrow; //角标图标
    private GameObject arrowObj;

    public override void Awake()
    {
        base.Awake();
        mTarget = target as HMI_SlideButton;

        mSliceButton = mTarget.transform.Find("SlideButton").GetComponent<SlideButton>();
        mLayoutRoot = mSliceButton.GetComponent<RectTransform>();
        mBar = mTarget.transform.Find("Bar").GetComponent<RectTransform>();
        arrowObj = mTarget.transform.Find("Arrow")?.gameObject;

        hasArrow = (arrowObj != null);

        mTarget.mTexts = new List<string>(mLayoutRoot.childCount);
        for (int i = 0; i < mLayoutRoot.childCount; i++)
        {
            var text = mLayoutRoot.GetChild(i).transform.Find("TextItem").GetComponent<Text>();
            mTarget.mTexts.Add(text.text);
        }
    }
    private void OnEnable()
    {
        Texts = serializedObject.FindProperty("mTexts");
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        EditorGUI.BeginChangeCheck();
        EditorGUILayout.PropertyField(Texts);
        hasArrow = EditorGUILayout.Toggle("有角标：", hasArrow);
        mSliceButton.IsHorizontal = EditorGUILayout.Toggle("横向布局：", mSliceButton.IsHorizontal);

        if (EditorGUI.EndChangeCheck())
        {
            var voiceText = "";
            if (mTarget.mTexts != null)
            {
                for (int i = 0; i < mTarget.mTexts.Count; i++)
                {
                    voiceText += (mTarget.mTexts[i] + "|");
                }
            }
            mTarget.SetVoiceText(voiceText);

            if (mSliceButton.IsHorizontal)
            {
                var vertical = mSliceButton.GetComponent<VerticalLayoutGroup>();
                if (vertical != null)
                {
                    DestroyImmediate(vertical);
                }
                if (mSliceButton.GetComponent<HorizontalLayoutGroup>() == null)
                {
                    var horizontal = mSliceButton.gameObject.AddComponent<HorizontalLayoutGroup>();
                    horizontal.padding.left = 3;
                    horizontal.padding.right = 3;
                    horizontal.padding.top = 8;
                    horizontal.padding.bottom = 8;
                    horizontal.childAlignment = TextAnchor.MiddleLeft;
                    horizontal.childControlWidth = true;
                    horizontal.childControlHeight = true;
                    horizontal.childScaleWidth = false;
                    horizontal.childScaleHeight = true;
                    horizontal.childForceExpandWidth = true;
                    horizontal.childForceExpandHeight = true;
                }
            }
            else
            {
                var horizontal = mSliceButton.GetComponent<HorizontalLayoutGroup>();
                if (horizontal != null)
                {
                    DestroyImmediate(horizontal);
                }
                if (mSliceButton.GetComponent<VerticalLayoutGroup>() == null)
                {
                    var vertical = mSliceButton.gameObject.AddComponent<VerticalLayoutGroup>();
                    vertical.padding.left = 8;
                    vertical.padding.right = 8;
                    vertical.padding.top = 3;
                    vertical.padding.bottom = 3;
                    vertical.childAlignment = TextAnchor.MiddleLeft;
                    vertical.childControlWidth = true;
                    vertical.childControlHeight = true;
                    vertical.childScaleWidth = false;
                    vertical.childScaleHeight = true;
                    vertical.childForceExpandWidth = true;
                    vertical.childForceExpandHeight = true;
                }
            }

            EditorUtility.SetDirty(mTarget);

            var rect = mLayoutRoot.GetChild(0) as RectTransform;
            if (mBar.sizeDelta != rect.sizeDelta)
            {
                mBar.sizeDelta = rect.sizeDelta;
                EditorUtility.SetDirty(mBar);
                EditorUtility.SetDirty(mTarget);
            }
        }
        RefreshTexts();
        RefreshObj();
    }

    protected override string GetDataJson()
    {
        var data = new HMI_SlideButtonData();

        data.HasArrow = hasArrow;
        data.mTexts = mTarget.mTexts;

        string json = JsonUtility.ToJson(data, true);
        return json;
    }

    protected override void SetData(string json)
    {
        HMI_SlideButtonData data = JsonUtility.FromJson<HMI_SlideButtonData>(json);

        hasArrow = data.HasArrow;
        RefreshObj();
        mTarget.mTexts = data.mTexts;
        RefreshTexts();

        base.SetData(json);
        EditorUtility.SetDirty(mTarget);
    }

    private void RefreshTexts()
    {
        mSliceButton.allButton.Clear();
        var oldCount = mLayoutRoot.childCount;
        if (mTarget.mTexts == null || mTarget.mTexts.Count <= 0)
        {
            mLayoutRoot.GetChild(0).gameObject.SetActive(false);
            for (int i = 1; i < oldCount; i++)
            {
                DestroyImmediate(mLayoutRoot.GetChild(i).gameObject);
            }
        }
        else
        {
            if (oldCount > mTarget.mTexts.Count)
            {
                for (int i = mTarget.mTexts.Count; i < oldCount; i++)
                {
                    if (i < mLayoutRoot.childCount)
                    {
                        DestroyImmediate(mLayoutRoot.GetChild(i).gameObject);
                    }
                }
            }
            else if (oldCount < mTarget.mTexts.Count)
            {
                var delta = mTarget.mTexts.Count - oldCount;
                for (int i = 0; i < delta; i++)
                {
                    Instantiate(mLayoutRoot.GetChild(0).gameObject, Vector2.zero, Quaternion.identity, mLayoutRoot);
                }
            }

            for (int i = 0; i < mTarget.mTexts.Count; i++)
            {
                var item = mLayoutRoot.GetChild(i);
                item.gameObject.SetActive(true);
                item.gameObject.name = mTarget.mTexts[i];
                var text = item.Find("TextItem").GetComponent<Text>();
                text.text = mTarget.mTexts[i];
                mSliceButton.allButton.Add(item.GetComponent<Button>());
            }
        }
    }

    private void RefreshObj()
    {
        if (hasArrow)
        {
            if (arrowObj == null)
            {
                arrowObj = HMI_Slider_Editor.LoadMiniWidget(mTarget.transform, "Arrow");
            }
        }
        else
        {
            var arrowGo = mTarget.transform.Find("Arrow");
            if (arrowGo != null)
            {
                arrowObj = null;
                DestroyImmediate(arrowGo.gameObject);
            }

        }
    }
}

[Serializable]
public class HMI_SlideButtonData
{
    public bool HasArrow;  //是否有角标
    public List<string> mTexts; //按钮列表
}
